package com.ece750.server;

import com.ece750.client.GTalkService;
import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.MessageBuilder;
import com.google.appengine.api.xmpp.MessageType;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class GTalkServerImpl extends RemoteServiceServlet implements
	GTalkService {
	
	private static final XMPPService xmppService =
		      XMPPServiceFactory.getXMPPService();
	
	public String sendMessage(String message) throws IllegalArgumentException {
		//Send the message to the google talk user
		JID sendTo = new JID("david.drich@gmail.com");
		
		//Also use the channelservice to send it back to the user
		//ChannelService s = ChannelServiceFactory.getChannelService();
		//s.sendMessage(new ChannelMessage("d4dietri", message));
		
		Message newMessage = new MessageBuilder()
			.withRecipientJids(sendTo)
			.withMessageType(MessageType.CHAT)
			.withBody(message)
			.build();
		
		xmppService.sendMessage(newMessage);
		
		return "good";
	}
	
	public String sendVidChatNotification(String username, String sessionId) throws IllegalArgumentException {
		JID sendTo = new JID("david.drich@gmail.com");
		
		Message msg = new MessageBuilder()
			.withRecipientJids(sendTo)
			.withMessageType(MessageType.CHAT)
			.withBody(username  + " would like to video chat with you: http://www.helloworld-david.appspot.com/vidchat.jsp?sessionId=" + sessionId)
			.build();
		
		xmppService.sendMessage(msg);
		
		return "good";
	}
	
	public String getToken(String username) throws IllegalArgumentException {
		ChannelService channelService = ChannelServiceFactory.getChannelService();
		String token = channelService.createChannel("d4dietri");
		return token;
	}
}
